<?php
/** 
 * @category		
 * @package		Lib			
 * @subpackage	Action	
 * @author		wanght <whtaok@gmail.com>
 * @since		2013-3-2
 * @version		$Id: BaseAction.class.php 35 2013-03-05 14:35:28Z whtaok@gmail.com $
 */

class BaseAction extends AppAction {
	/**
	 *
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected  function _initialize() {
		//头部菜单
		$role_id = 1;
		$menu_type = 'admin';
		$menu_id = 2;
		$this->assign('menu_top',$this->getMenuTop($role_id,$menu_type));
		$this->assign('menu_sub',$this->getMenuSub($role_id,$menu_type,$menu_id));
	}

	/**
	 * 初始化树状菜单
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected function initMenuTree($role_id,$menu_type) {
		//1.根据角色取的菜单列表
		$model	=	D('AccessMenuView');
		$menu	=	$model->getMenuByRoleId($role_id);
		
		//2.生成菜单树型数组
		$menu_tree = array();
		$menu_rawtree = array();
		foreach ($menu as $item) {
			//原始数组
			$menu_rawtree[$item["menu_id"]] = $item;
		}
		unset($menu);

		foreach ($menu_rawtree as $item) {
			//树状数组
			if($item["menu_pid"]==0){
				$menu_tree[$item['menu_id']]=&$menu_rawtree[$item['menu_id']];
			}else{
				$menu_rawtree[$item['menu_pid']]["children"][]=&$menu_rawtree[$item['menu_id']];
			}
		}
		unset($menu_rawtree);

		//3.文件缓存数组
		F('menu_tree'.$role_id.$menu_type,$menu_tree);
		//4.返回树状数组
		return $menu_tree;
	}

	/**
	 * 提取头部菜单数组
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected function getMenuTop($role_id,$menu_type){
		//提取树型菜单数组
		$menu_tree = F('menu_tree'.$role_id.$menu_type);
		if(!$menu_tree){
			$menu_tree = $this->initMenuTree($role_id,$menu_type);
		}
		
		//提取头部菜单数组
		$menu_top = array();
		foreach ($menu_tree as $item) {
			$menu_top[] = array('menu_id'=>$item['menu_id'],'menu_title'=>$item['menu_title']);
		}
		return $menu_top;
	}

	/**
	 * 提取子菜单数组
	 * @access		protected
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	protected function getMenuSub($role_id,$menu_type,$menu_id){
		//提取树型菜单数组
		$menu_tree = F('menu_tree'.$role_id.$menu_type);
		if(!$menu_tree){
			$menu_tree = $this->initMenuTree($role_id,$menu_type);
		}

		//提取子菜单数组
		$menu_sub = $menu_tree[$menu_id]['children'];
		return $menu_sub;
	}
}

?>